package net.maku.system.erp.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.system.erp.convert.SysWarehouseConvert;
import net.maku.system.erp.entity.SysWarehouseEntity;
import net.maku.system.erp.query.SysWarehouseQuery;
import net.maku.system.erp.service.SysWarehouseService;
import net.maku.system.erp.vo.SysWarehouseVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("erp/warehouse")
@Tag(name = "仓库管理")
@AllArgsConstructor
public class SysWarehouseController {
    private final SysWarehouseService warehouseService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<SysWarehouseVO>> page(@ParameterObject @Valid SysWarehouseQuery query){
        PageResult<SysWarehouseVO> page = warehouseService.page(query);

        return Result.ok(page);
    }
    @GetMapping("list")
    @Operation(summary = "列表")
    public Result<List<SysWarehouseVO>> list() {
        List<SysWarehouseVO> list = warehouseService.getList();

        return Result.ok(list);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<SysWarehouseVO> get(@PathVariable("id") Long id) {
        SysWarehouseEntity entity = warehouseService.getById(id);
        SysWarehouseVO vo = SysWarehouseConvert.INSTANCE.convert(entity);

        return Result.ok(vo);
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    public Result<String> save(@RequestBody @Valid SysWarehouseVO vo) {
        warehouseService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    public Result<String> update(@RequestBody @Valid SysWarehouseVO vo) {
        warehouseService.update(vo);

        return Result.ok();
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    public Result<String> delete(@PathVariable("id") Long id) {
        warehouseService.delete(id);

        return Result.ok();
    }

}